//
//  KYRadioBoxChooseView.m
//  KYKJPackDemo
//
//  Created by Jordan zhou on 2017/10/11.
//  Copyright © 2017年 ???. All rights reserved.
//

#define KYKeyBoardHeight 216

#import "KYRadioBoxChooseView.h"

@interface KYRadioBoxChooseView()<UIPickerViewDelegate,UIPickerViewDataSource>
/**
 *  背景view
 */
@property (nonatomic, weak) UIView *bgView;
/**
 *  显示view
 */
@property (nonatomic, weak) UIView *contentView;
/**
 *  取消按钮
 */
@property (nonatomic, weak) UIButton *cancelBtn;
/**
 *  确认按钮
 */
@property (nonatomic, weak) UIButton *sureBtn;
/**
 *  选择控件
 */
@property (nonatomic, weak) UIPickerView *pickerView;
/**
 *  选择的内容
 */
@property (nonatomic, weak) NSString *str;
/**
 *  按钮背景view
 */
@property (nonatomic, weak) UIView *buttonBgView;
/**
 *  lineview
 */
@property (nonatomic, weak) UIView *lineView;
@end

@implementation KYRadioBoxChooseView


- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        [self setUpSubViews];
    }
    return self;
}

- (instancetype)initWithCoder:(NSCoder *)decoder
{
    if (self = [super initWithCoder:decoder]) {
        [self setUpSubViews];
    }
    return self;
}

/**
 *  初始化\进行一次性设置
 */
- (void)setUpSubViews
{
    UIView *bgView = [[UIView alloc] init];
    bgView.backgroundColor = [UIColor blackColor];
    bgView.alpha = 0.5;
    [self addSubview:bgView];
    self.bgView = bgView;
    
    UIView *contentView = [[UIView alloc] init];
    contentView.backgroundColor = [UIColor whiteColor];
    [self addSubview:contentView];
    self.contentView = contentView;
    
    UIView *buttonBgView = [[UIView alloc]init];
    buttonBgView.backgroundColor = [UIColor whiteColor];
    buttonBgView.alpha = 0.0f;
    
    UIView *lineView = [[UIView alloc]init];
    lineView.backgroundColor = UIColorFromRGB(0xEEEFF1);
    [buttonBgView addSubview:lineView];
    self.lineView = lineView;
    [self addSubview:buttonBgView];
    self.buttonBgView = buttonBgView;
    
    UIButton *cancelBtn = [[UIButton alloc] init];
    cancelBtn.backgroundColor = [UIColor clearColor];
    cancelBtn.titleLabel.font = [UIFont boldSystemFontOfSize:14];
    cancelBtn.alpha = 0.0f;
    [cancelBtn setTitle:NSLocalizedString(@"取消", nil) forState:UIControlStateNormal];
    [cancelBtn setTitleColor:UIColorFromRGB(0x4a7ff0) forState:UIControlStateNormal];
    [cancelBtn addTarget:self action:@selector(hide) forControlEvents:UIControlEventTouchUpInside];
    [self.buttonBgView addSubview:cancelBtn];
    self.cancelBtn = cancelBtn;
    
    UIButton *sureBtn = [[UIButton alloc] init];
    sureBtn.backgroundColor = [UIColor clearColor];
    sureBtn.titleLabel.font = [UIFont boldSystemFontOfSize:14];
    sureBtn.alpha = 0.0f;
    [sureBtn setTitle:NSLocalizedString(@"确定", nil) forState:UIControlStateNormal];
    [sureBtn setTitleColor:UIColorFromRGB(0x4a7ff0) forState:UIControlStateNormal];
    [sureBtn addTarget:self action:@selector(didChoose) forControlEvents:UIControlEventTouchUpInside];
    [self.buttonBgView addSubview:sureBtn];
    self.sureBtn = sureBtn;
    
    UIPickerView *pickerView = [[UIPickerView alloc] init];
    pickerView.delegate = self;
    pickerView.dataSource = self;
    [contentView addSubview:pickerView];
    self.pickerView = pickerView;
    
    // 添加手势
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(triggerTapGesture:)];
    [bgView addGestureRecognizer:tapGesture];
}

/**
 *  点击弹出框以外的部分, 则收起当前弹出框对应的View
 *
 *  @param gestureRecognizer UITapGestureRecognizer
 */
- (void)triggerTapGesture:(UITapGestureRecognizer *)gestureRecognizer {
    
    [self hide];
}

/**
 *  点击按钮通知代理拿到选择
 */
- (void)didChoose
{
    if ([self.delegate respondsToSelector:@selector(radioBoxChooseView:didChooseStr:)]) {
        [self.delegate radioBoxChooseView:self didChooseStr:self.str];
    }
    
    [self hide];
}

/**
 *  拿到真实尺寸进行设置
 */
- (void)layoutSubviews
{
    [super layoutSubviews];
    
    CGFloat bgViewX = 0;
    CGFloat bgViewY = 0;
    CGFloat bgViewW = self.frame.size.width;
    CGFloat bgViewH = self.frame.size.height;
    self.bgView.frame = CGRectMake(bgViewX, bgViewY, bgViewW, bgViewH);
    
    CGFloat contentViewX = 0;
    CGFloat contentViewH = KYKeyBoardHeight;
    CGFloat contentViewY = bgViewH - contentViewH;
    CGFloat contentViewW = bgViewW;
    self.contentView.frame = CGRectMake(contentViewX, contentViewY, contentViewW, contentViewH);
    
    CGFloat cancelBtnW = 50;
    CGFloat cancelBtnX = 10;
    CGFloat cancelBtnH = 30;
    CGFloat cancelBtnY = 5;
    self.cancelBtn.frame = CGRectMake(cancelBtnX, cancelBtnY, cancelBtnW, cancelBtnH);
    
    CGFloat sureBtnW = 50;
    CGFloat sureBtnX = bgViewW - sureBtnW - 10;
    CGFloat sureBtnH = 30;
    CGFloat sureBtnY =  5;
    self.sureBtn.frame = CGRectMake(sureBtnX, sureBtnY, sureBtnW, sureBtnH);
    
    CGFloat buttonBgViewX = 0;
    CGFloat buttonBgViewY = CGRectGetMaxY(self.contentView.frame) - KYKeyBoardHeight - sureBtnH - 10;
    CGFloat buttonBgViewW = bgViewW;
    CGFloat buttonBgViewH = 40;
    self.buttonBgView.frame = CGRectMake(buttonBgViewX, buttonBgViewY, buttonBgViewW, buttonBgViewH);
    
    CGFloat lineViewX = 0;
    CGFloat lineViewH = 1;
    CGFloat lineViewW = bgViewW;
    CGFloat lineViewY = buttonBgViewH - 1;
    self.lineView.frame = CGRectMake(lineViewX, lineViewY, lineViewW, lineViewH);
    
    CGFloat pickerDateViewX = 0;
    CGFloat pickerDateViewH = contentViewH;
    CGFloat pickerDateViewY = 0;
    CGFloat pickerDateViewW = contentViewW;
    self.pickerView.frame = CGRectMake(pickerDateViewX, pickerDateViewY, pickerDateViewW, pickerDateViewH);
    [self pickerView:self.pickerView didSelectRow:0 inComponent:0]; // 默认选中第一个手动调一次否则就没有值嘛
}

/**
 *  展示
 */
- (void)show
{
    [[[UIApplication sharedApplication].delegate window] addSubview:self];
    
    [UIView animateWithDuration:0.25 animations:^{
        self.contentView.transform = CGAffineTransformMakeTranslation(0, -(2 * KYKeyBoardHeight));
        self.buttonBgView.transform = CGAffineTransformMakeTranslation(0, -(2 * KYKeyBoardHeight));
        self.sureBtn.transform = CGAffineTransformMakeTranslation(0, -(2 * KYKeyBoardHeight));
        self.sureBtn.alpha = 1.0f;
        self.cancelBtn.alpha = 1.0f;
        self.buttonBgView.alpha = 1.0f;
    }];
}

/**
 *  隐藏
 */
- (void)hide
{
    [UIView animateWithDuration:0.25 animations:^{
        
        self.cancelBtn.alpha = 0.0f;
        self.sureBtn.alpha = 0.0f;
        self.contentView.alpha = 0.0f;
        self.buttonBgView.alpha = 0.0f;
        self.bgView.alpha = 0.0f;
        
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}

#pragma mark - 数据源方法
/**
 *  一共多少列
 */
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 1;
}

/**
 *  第component列显示多少行
 */
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    return self.dataArr.count;
}

#pragma mark - 代理方法
/**
 *  第component列的第row行显示什么文字
 */
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    NSString *str = [self.dataArr objectAtIndex:row];
    return str;
}

/**
 *  这个方法中拿到值
 */
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    NSString *str = [self.dataArr objectAtIndex:row];
    self.str = str;
}
@end
